<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Exception;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Shop extends Api
{

    /**
     * Shop模型对象
     * @var \app\admin\model\Shop
     */
    protected $model = null;
    protected $userModel = null;

    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Shop;
        $this->userModel = new \app\admin\model\Shop;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 店铺列表
     * @return void
     */
    public function index()
    {
        $params = $this->request->param();
        try {
            $where = [];
            if (!empty($params['status'])) {
                $where['status'] = $params['status'];
            }
            $list = $this->model->where($where)->paginate($params['page_size']);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('查询成功', $list);
    }

    /**
     * 申请店铺
     * @return void
     */
    public function apply()
    {
        $params = $this->request->post();
        try {
            //是否采用模型验证
            $validate_class = '\app\admin\validate\Shop';
            $validate = validate($validate_class);
            if (!$validate->check($params, [], "add")) {
                throw new Exception($validate->getError());
            }
            $shop = $this->model->where('user_id', $this->auth->id)->find();

            if ($shop) {
                throw new Exception('当前账号已经申请店铺');
            }


            $data = $this->model->where('shopname', $params['shopname'])->find();
            if ($data) {
                throw new Exception('店铺名称重复');
            }


            $params['user_id'] = $this->auth->id;
            $params['status'] = 1;
            $shopData = $this->model->allowField(true)->create($params);
            $this->userModel->allowField(true)->save(['shop_id', $shopData['id']], ['id' => $this->auth->id]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('申请成功，等待审核中...');
    }

    /**
     * 处理申请店铺
     * @return void
     */
    public function check()
    {
        $params = $this->request->post();
        try {
            $data = $this->model->where('id', '=', $params['id'])->find();

            if (empty($data)) {
                throw new Exception('当前店铺不存在');
            }

            if ($data['status'] == 2) {
                throw new Exception('当前店铺已经审核通过');
            }
            $params['apply_time'] = time();
            $data->save($params);
            $message = $params['status'] == 2 ? '审核通过' : '审核驳回';
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success($message);
    }

    /**
     * 店铺详情
     * @return void
     */
    public function info()
    {
        $params = $this->request->param();

        try {
            //是否采用模型验证
            $validate_class = '\app\admin\validate\Shop';
            $validate = validate($validate_class);
            if (!$validate->check($params, [], "info")) {
                throw new Exception($validate->getError());
            }
            $data = $this->model->where('id', $params['id'])->find();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('查询成功', $data);
    }
}
